/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.persistence.mapping;

import java.util.List;

import org.telosys.common.TelosysRuntimeException;

/**
 * Mapping definition for an entity
 * 
 * @author Laurent Guerin
 *
 * @param <T>
 */
public class EntityMapping<T>  {

	public final static boolean  PRIMARY_KEY     = true;
	public final static boolean  NOT_PK          = false;
	
	public final static boolean  GENERATED_VALUE = true ;
	public final static boolean  NOT_GENERATED   = false ;
	
	private final Class<T> entityClass ; 
	
	private final String table ;
	
	private final String schema ;
	
	private final AttributeMapping[]  attributesMapping ;
	
	private final List<LinkMapping>   linksToOneMapping ;
	private final List<LinkMapping>   linksToManyMapping ;
	
	/**
	 * Constructor
	 * @param entityClass
	 * @param table
	 * @param schema
	 * @param attributesMapping
	 * @param linksToOneMapping
	 * @param linksToManyMapping
	 */
	public EntityMapping(Class<T> entityClass, String table, String schema, AttributeMapping[] attributesMapping, 
			List<LinkMapping> linksToOneMapping, List<LinkMapping> linksToManyMapping  ) 
	{
		super();
		
		if ( null == entityClass ) throw new TelosysRuntimeException("Entity class is null");
		if ( null == table ) throw new TelosysRuntimeException("Table name is null");
		// Schema can be null
		if ( null == attributesMapping ) throw new TelosysRuntimeException("Key columns mapping is null");
		// Links mapping can be null 
		
		this.entityClass = entityClass;
		this.table = table;
		this.schema = schema;
		this.attributesMapping  = attributesMapping;
		this.linksToOneMapping  = linksToOneMapping ;
		this.linksToManyMapping = linksToManyMapping ;
	}

	public Class<T> getEntityClass() {
		return entityClass;
	}

	public String getTable() {
		return table;
	}

	public String getSchema() {
		return schema;
	}

	public AttributeMapping[] getAttributesMapping() {
		return attributesMapping;
	}

	public List<LinkMapping> getLinksToOneMapping() {
		return linksToOneMapping;
	}

	public List<LinkMapping> getLinksToManyMapping() {
		return linksToManyMapping;
	}

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("Mapping for " );
		sb.append( entityClass != null ? entityClass.getSimpleName() : "null (no entity class !)") ;
		sb.append(" : table = '" );
		sb.append( this.table ) ;
		sb.append("', schema = '" );
		sb.append( this.schema ) ;
		sb.append("', " );
		sb.append(attributesMapping != null ? attributesMapping.length : 0 );
		sb.append(" fields" );
		
		return sb.toString();
	}
}
